$(document).ready(function () {
	// 获取畅销榜数据
	$.ajax({
		type: 'POST',
		url: 'http://10.2.1.17:8099/EXOnline/font/courses?method=leaderboard&boardType=best_selling',
		success: function (res) {
			console.log(res);
			// 过滤价格为0的课程
			const topSellingCourses = res.data.filter(course => course.course_price > 0);
			// 更新畅销榜
			updateRankings(topSellingCourses, '.rank-card', 'sales_count'); // 传入数据和目标选择器
		},
		error: function (error) {
			console.log(error);
		}
	});

	// 获取学习榜数据
	$.ajax({
		type: 'POST',
		url: 'http://10.2.1.17:8099/EXOnline/font/courses?method=leaderboard&boardType=most_studied',
		success: function (res) {
			console.log(res);
			// 获取所有数据
			const topStudiedCourses = res.data;
			// 更新学习榜
			updateRankings(topStudiedCourses, '.study-rank', 'student_count'); // 传入数据和目标选择器
		},
		error: function (error) {
			console.log(error);
		}
	});

	// 更新排行榜的函数
	function updateRankings(data, selector, countField) {
		const courseList = $(selector).find('.course-list');
		courseList.empty(); // 清空现有课程

		// 只取前3条数据
		const limitedData = data.slice(0, 3);

		limitedData.forEach((course, index) => {
			const courseItem = `
                <a href="#" class="course-item">
                    <div class="rank-number">${index + 1}</div>
                    <div class="course-image">
                        <img src="img/diushi.jpeg" alt="课程图片">
                    </div>
                    <div class="course-info">
                        <div class="course-title">${course.course_name}</div>
                        <div class="course-price">¥${course.course_price}</div>
                        <div class="student-count">${countField === 'student_count' ? course.student_count : course.sales_count}人在学</div>
                    </div>
                </a>
            `;
			courseList.append(courseItem); // 添加课程项
		});
	}
});